package com.smartleanx.module.hrm.util.esign;

import com.smartleanx.module.hrm.util.esign.exception.EsignDemoException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @description  文件基础信息封装类
 * @author  澄泓
 * @date  2020/10/26 14:54
 * @version JDK1.7
 */
public class EsignFileBean {
    //文件名称
    private String fileName;
    //文件大小
    private int fileSize;
    //文件内容MD5
    private String fileContentMD5;
    //文件地址
    private String filePath;


    public EsignFileBean(String filePath) throws EsignDemoException {
        String fileName = "";
        long contentLengthLong = 0L;
        HttpURLConnection connection = null;
        try {
            URL url = new URL(filePath);
            String path = url.getPath();
            fileName = path.substring(path.lastIndexOf('/') + 1);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("HEAD");
            int responseCode = connection.getResponseCode();
            if(responseCode != HttpURLConnection.HTTP_OK){
                throw new EsignDemoException("文件不存在");
            }
            contentLengthLong = connection.getContentLengthLong();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(connection != null){
                connection.disconnect();
            }
        }
        this.filePath=filePath;
        this.fileContentMD5 = FileTransformation.getFileContentMD5(filePath);
        this.fileName = fileName;
        this.fileSize = (int) contentLengthLong;
    }

    public String getFileName() {
        return fileName;
    }

    public int getFileSize() {
        return fileSize;
    }

    public String getFileContentMD5() {
        return fileContentMD5;
    }

    /**
     * 传入本地文件地址获取二进制数据
     * @return
     * @throws EsignDemoException
     */
    public byte[] getFileBytes() throws Exception {
        System.out.println("文件路径："+filePath);
        return FileTransformation.fileToBytes(filePath);
    }

    public byte[] getBytesFromUrl(String fileUrl) throws Exception {
        HttpURLConnection connection = null;
        InputStream inputStream = null;
        ByteArrayOutputStream outputStream = null;

        try {
            URL url = new URL(fileUrl);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            int responseCode = connection.getResponseCode();
            if (responseCode != HttpURLConnection.HTTP_OK) {
                throw new EsignDemoException("远程文件获取失败，HTTP响应码: " + responseCode);
            }

            inputStream = connection.getInputStream();
            outputStream = new ByteArrayOutputStream();

            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            return outputStream.toByteArray();

        } catch (Exception e) {
            throw new EsignDemoException("获取远程文件字节流失败", e);
        } finally {
            try {
                if (inputStream != null) inputStream.close();
                if (outputStream != null) outputStream.close();
                if (connection != null) connection.disconnect();
            } catch (Exception e) {
                throw new EsignDemoException("关闭资源失败", e);
            }
        }
    }
}
